package com.superb.soslon.controller;

import com.superb.core.domain.enums.FlowCommentType;
import com.superb.core.domain.model.Result;
import com.superb.core.domain.params.FlowExecuteParam;
import com.superb.core.domain.params.FlowStartParam;
import com.superb.core.service.EasyProcessInstanceService;
import com.superb.core.service.EasyTaskService;
import com.superb.soslon.entity.SickLeave;
import org.flowable.task.api.Task;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

/**
 * @author MoJie
 * @since 1.0
 */
@Controller
public class FormFlowController {

    @Inject
    private EasyProcessInstanceService processInstanceService;
    @Inject
    private EasyTaskService easyTaskService;

    /**
     * 启动一个表单流程
     * @return {@link Result} {@link ?}
     * @author MoJie
     */
    @Get
    @Mapping("startForm")
    public Result<?> startForm() {
        // 实体需要
        SickLeave leave = new SickLeave();
        leave.setId("001");
        leave.setTitle("差旅费报销");
        leave.setReason("出差：北京->宁波->贵阳 往返");
        leave.setStartTime("2024-11-23 09:00:00");
        leave.setEndTime("2024-11-28 12:00:00");
        leave.setRemarks("+餐补+食宿");
        FlowStartParam startParam = new FlowStartParam();
        startParam.setStartFormData(true); // 开启流程表单
        startParam.setFlowKey("bxsp"); // 上面创建模型的key,会使用最新部署的流程
        startParam.setStartUserId("9527"); // 流程发起人
        startParam.setStartUsername("solon-flowable");  // 流程发起人名称
        startParam.setProcessName("北京出差(9527)-差旅费报销");
        startParam.setBusinessKey("001"); // 业务主键，要与提交的实体有关联
        startParam.setSkipFirstNode(true); // 是否跳过开始节点-执行第一用户节点
        String processInstanceId = processInstanceService.startProcessInstanceByKey(startParam);
        // 后续的执行操作都需要这个id
        System.out.println("流程实例ID： " + processInstanceId);
        return Result.success(processInstanceId);
    }

    /**
     * 重新提交该任务，任务id和实力id都可以通过ui控制台获取
     * @param taskId 任务ID
     * @param processInstanceId 流程实例ID，流程启动时获得
     * @return {@link Result} {@link ?}
     * @author MoJie
     */
    @Get
    @Mapping("resubmitForm")
    public Result<?> resubmitForm(String taskId, String processInstanceId) {
        // 任务执行后。任务回到发起人手里，发起人就需要重新提交，重新提交就需要重新填写资料
        SickLeave leave = new SickLeave();
        leave.setId("001");
        leave.setTitle("差旅费报销");
        leave.setReason("出差：北京->宁波->贵阳 往返");
        leave.setStartTime("2024-11-23 09:00:00");
        leave.setEndTime("2024-11-28 12:00:00");
        leave.setRemarks("餐补+食宿+娱乐");
        FlowExecuteParam resubmit = new FlowExecuteParam();
        resubmit.setTaskId(taskId); // 执行任务的ID
        resubmit.setProcessInstanceId(processInstanceId); // 执行实例
        resubmit.setAssignee("9527"); // 任务执行人，任务执行人需要正确，否则会抛出异常
        resubmit.setAssigneeName("solon-flowable"); // 执行人姓名
        resubmit.setFlowCommentType(FlowCommentType.RESUBMIT); // 重新提交
        resubmit.setForm(true); // 表单
        resubmit.setCommentContent(leave); // 驳回原因
        easyTaskService.executeNextStep(resubmit);
        return Result.success();
    }

}
